#include "myinputpanelcontext.h"
#include "main.h"
#include "global.h"

void init_data();
void customMessageHandler(QtMsgType type, const char *msg)  ;



int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(src);
   QApplication a(argc, argv);
   /*
   QTextCodec *gbk = QTextCodec::codecForName("GBK");
   QTextCodec::setCodecForTr(gbk);
   QTextCodec::setCodecForCStrings(gbk);
   QTextCodec::setCodecForLocale(gbk);
   QTextCodec::setCodecForTr(gbk);
   QTextCodec::setCodecForLocale(gbk);
   QTextCodec::setCodecForCStrings(gbk);*/
   //这个方法注册回调函数
   qInstallMsgHandler(customMessageHandler);
   MyInputPanelContext *ic = new MyInputPanelContext;
   a.setInputContext(ic);

   QCursor cursor(Qt::BlankCursor);
   a.setOverrideCursor(cursor);

   GPIO_SETUP();
   pwm=new PWM();
   pwm->pwm_tweet(1);

   read_config(DATA_FILE);

  // data_acquisation AD_data;//？由实例变量改为指针变量后工作正常，怀疑是超出堆栈大小，
   textbrowser=new QTextBrowser;
   AD_data=new data_acquisation;
   AD_data->start();

   if(!whole_machine_parameter.limits_unlock) certificate();

     welcom w;
     input=new Password_input;
     if(input->password_check("1234",true)) w.showFullScreen();

     init_data();
     qDebug() <<"app started!";

     int ret= a.exec();
     monitor_program->stop();
     AD_data->stop();
     delete pwm;
     delete stype; 
     delete serialprinter;
     delete controller;
     delete monitor_program;
     delete messagebox;
     delete input;//Illegal instruction
     return ret;

}

void init_data()
{

    backdoor_canopen=false;
    water_level_failure=false;
    door_detection_failure=false;
    water_valve_isopen=false;
    water_supply_isopen=false;
    heating_valve_isopen=false;
    Vacuum_pump_isopen=false;
    air_supply_valve_isopen=false;
    air_intake_valve_isopen=false;
    exhaust_valve_isopen=false;
    sensor_data_changed=true;

    if(IO_setting_datamap.size()==0) sterilization_default_setting_recovery(IO_setting_datamap,4);
    MCD_Interface->read_IO_setting_data(IO_setting_datamap);
}

#include <QtDebug>
#include <QFile>
#include <QTextStream>
 //回调函数实现debug信息到文件
void customMessageHandler(QtMsgType type, const char *msg)
{
    QString txt;
    switch (type) {
    case QtDebugMsg:
        txt = QString("Debug: %1").arg(msg);
        break;

    case QtWarningMsg:
        txt = QString("Warning: %1").arg(msg);
    break;
    case QtCriticalMsg:
        txt = QString("Critical: %1").arg(msg);
    break;
    case QtFatalMsg:
        txt = QString("Fatal: %1").arg(msg);
        abort();
    }

    QFile outFile("/qt/log");
    outFile.open(QIODevice::WriteOnly | QIODevice::Append );//|QIODevice::Unbuffered
    QTextStream ts(&outFile);
    ts << txt <<"------" <<QDateTime::currentDateTime().toString() << endl;
}
